/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.MessageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Controller - 消息
 */
@Controller("adminMessageController")
@RequestMapping("/asl-admin/message")
public class MessageController extends BaseController {

    @Resource(name = "messageServiceImpl")
    MessageService messageService;
    @Resource(name = "memberServiceImpl")
    MemberService memberService;

    /**
     * 检查用户名是否合法
     */
    @RequestMapping(value = "/check_username", method = RequestMethod.GET)
    public
    @ResponseBody
    boolean checkUsername(String username) {
        return memberService.usernameExists(username);
    }

    /**
     * 发送
     */
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public String send(Long draftMessageId, Model model) {
        com.hboxs.asl.entity.Message draftMessage = messageService.find(draftMessageId);
        if (draftMessage != null && draftMessage.getIsDraft() && draftMessage.getSender() == null) {
            model.addAttribute("draftMessage", draftMessage);
        }
        return "admin/message/send";
    }

    /**
     * 发送
     */
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public String send(Long draftMessageId, String username, String title, String content, @RequestParam(defaultValue = "false") Boolean isDraft, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        if (!isValid(com.hboxs.asl.entity.Message.class, "content", content)) {
            return ERROR_VIEW;
        }
        com.hboxs.asl.entity.Message draftMessage = messageService.find(draftMessageId);
        if (draftMessage != null && draftMessage.getIsDraft() && draftMessage.getSender() == null) {
            messageService.delete(draftMessage);
        }
        Member receiver = null;
        if (StringUtils.isNotEmpty(username)) {
            receiver = memberService.findByUsername(username);
            if (receiver == null) {
                return ERROR_VIEW;
            }
        }
        com.hboxs.asl.entity.Message message = new com.hboxs.asl.entity.Message();
        message.setTitle(title);
        message.setContent(content);
        message.setIp(request.getRemoteAddr());
        message.setIsDraft(isDraft);
        message.setSenderRead(true);
        message.setReceiverRead(false);
        message.setSenderDelete(false);
        message.setReceiverDelete(false);
        message.setSender(null);
        message.setReceiver(receiver);
        message.setForMessage(null);
        message.setReplyMessages(null);
        messageService.save(message);
        if (isDraft) {
            addFlashMessage(redirectAttributes, com.hboxs.asl.Message.success("admin.message.saveDraftSuccess"));
            return "redirect:draft.htm";
        } else {
            addFlashMessage(redirectAttributes, com.hboxs.asl.Message.success("admin.message.sendSuccess"));
            return "redirect:list.htm";
        }
    }

    /**
     * 查看
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(Long id, Model model) {
        com.hboxs.asl.entity.Message message = messageService.find(id);
        if (message == null) {
            return ERROR_VIEW;
        }
        model.addAttribute("adminMessage", message);
        return "/admin/message/view";
    }

    /**
     * 回复
     */
    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    public String reply(Long id, String content, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        if (!isValid(com.hboxs.asl.entity.Message.class, "content", content)) {
            return ERROR_VIEW;
        }
        com.hboxs.asl.entity.Message forMessage = messageService.find(id);
        if (forMessage == null || forMessage.getIsDraft() || forMessage.getForMessage() != null) {
            return ERROR_VIEW;
        }
        if ((forMessage.getSender() != null && forMessage.getReceiver() != null) || (forMessage.getReceiver() == null && forMessage.getReceiverDelete()) || (forMessage.getSender() == null && forMessage.getSenderDelete())) {
            return ERROR_VIEW;
        }
        com.hboxs.asl.entity.Message message = new com.hboxs.asl.entity.Message();
        message.setTitle("reply: " + forMessage.getTitle());
        message.setContent(content);
        message.setIp(request.getRemoteAddr());
        message.setIsDraft(false);
        message.setSenderRead(true);
        message.setReceiverRead(false);
        message.setSenderDelete(false);
        message.setReceiverDelete(false);
        message.setSender(null);
        message.setReceiver(forMessage.getReceiver() == null ? forMessage.getSender() : forMessage.getReceiver());
        if ((forMessage.getReceiver() == null && !forMessage.getSenderDelete()) || (forMessage.getSender() == null && !forMessage.getReceiverDelete())) {
            message.setForMessage(forMessage);
        }
        message.setReplyMessages(null);
        messageService.save(message);

        if (forMessage.getSender() == null) {
            forMessage.setSenderRead(true);
            forMessage.setReceiverRead(false);
        } else {
            forMessage.setSenderRead(false);
            forMessage.setReceiverRead(true);
        }
        messageService.update(forMessage);

        if ((forMessage.getReceiver() == null && !forMessage.getSenderDelete()) || (forMessage.getSender() == null && !forMessage.getReceiverDelete())) {
            addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
            return "redirect:view.htm?id=" + forMessage.getId();
        } else {
            addFlashMessage(redirectAttributes, com.hboxs.asl.Message.success("admin.message.replySuccess"));
            return "redirect:list.htm";
        }
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, Model model) {
        model.addAttribute("page", messageService.findPage(null, pageable));
        return "/admin/message/list";
    }

    /**
     * 草稿箱
     */
    @RequestMapping(value = "/draft", method = RequestMethod.GET)
    public String draft(Pageable pageable, Model model) {
        model.addAttribute("page", messageService.findDraftPage(null, pageable));
        return "/admin/message/draft";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public
    @ResponseBody
    com.hboxs.asl.Message delete(Long[] ids) {
        if (ids != null) {
            for (Long id : ids) {
                messageService.delete(id);
            }
        }
        return SUCCESS_MESSAGE;
    }

}